package com.project.ToDone;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Screen for creating a new evet
 * 
 * @author Matthew Guzdial
 *
 */
public class NewEventActivity extends Activity {

	/**
	 * Sets up links to the create event and cancel buttons, and
	 * creates the logic for what to do on presses. 
	 * 
	 */
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.new_event);
	    MasterControlProgram.saveAlteredEvent(this);
	
	    TextView cancelTextView = (TextView) findViewById(R.id.cancelTextView);
		
		//Listen for createEvent button
		Button createEvent = (Button)findViewById(R.id.createEventButton);
		createEvent.setOnClickListener(
				new View.OnClickListener() {
					private String name;
					private String description;
					private String time;
					private String date;
					private String type;
					private String location;
					private boolean recurring;
					
					/**
					 * This method checks to make sure that there is valid information
					 * input for event creation
					 * 
					 * @param View v
					 */
					public void onClick(View v) {
						EditText nameEdit = (EditText)findViewById(R.id.eventNameEdit);
						EditText dateEdit = (EditText)findViewById(R.id.dateEdit);
						EditText timeEdit = (EditText)findViewById(R.id.timeEdit);
						EditText locationEdit = (EditText)findViewById(R.id.locationEdit);
						EditText typeEdit = (EditText)findViewById(R.id.typeEdit);
						EditText descriptionEdit = (EditText)findViewById(R.id.descriptionEdit);
						CheckBox recurringCheck = (CheckBox)findViewById(R.id.recurringCheck);
						
						name = nameEdit.getText().toString();
						description = descriptionEdit.getText().toString();
						time = timeEdit.getText().toString();
						type = typeEdit.getText().toString();
						location= locationEdit.getText().toString();
						date = dateEdit.getText().toString();
						System.out.println("(" + name +")");
						//Returns true if checked, false otherwise
						recurring = recurringCheck.isChecked();
						
						RegexTester regex = new RegexTester();
						
						if(!regex.stringNotNullTest(name)){
							Toast.makeText(getApplicationContext(),
									"You need a name bro", Toast.LENGTH_SHORT).show();
						}
						else if(!regex.stringNotNullTest(date)){
							//Don't work
							Toast.makeText(getApplicationContext(),
									"You need a specific date bro", Toast.LENGTH_SHORT).show();
						}
						else if(!regex.stringNotNullTest(time)){
							//Don't work
							Toast.makeText(getApplicationContext(),
									"You need a specific time bro", Toast.LENGTH_SHORT).show();
						}
						else if(!regex.stringNotNullTest(type)){
							Toast.makeText(getApplicationContext(),
									"You need a type bro", Toast.LENGTH_SHORT).show();
						}
						else if(!regex.dateTest(date)){
							Toast.makeText(getApplicationContext(),
									"You need to format that date correctly bro", Toast.LENGTH_SHORT).show();
						}
						else if(!regex.timeTest(time)){
							Toast.makeText(getApplicationContext(),
									"You need to format that time correctly bro", Toast.LENGTH_SHORT).show();
						}
						else{
							Event e = new Event(recurring, false, date, time, type, location, name, description);
							MasterControlProgram.setNewEvent(e);
							
							Intent i = new Intent(getApplicationContext(),HomeActivity.class);
							startActivity(i);
						}
					
						
					}
				});
		
		//listen for cancel button
		cancelTextView.setOnClickListener(new View.OnClickListener() {
			
			/**
			 * This method closes the registration screen.
			 * 
			 * @param View v
			 */
			public void onClick(View v) {
				// Close registration screen
				finish();
				
			}
		});
	}
}
